﻿// TempFolderContents
// 
// Class for providing access to indices of indexed scan folders in temp folder
// 
// FLMScan - Featured by FabLab Munich

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FLMScan.Tools;
using log4net;

namespace FLMScan.Tools
{
    class TempFolderContents
    {
        private ILog logger;
        private IFileSystem fileSystem;
        private String tempFolderPath;

        private int nextFolderIndex = 1;
        private int smallestFolderIndex;
        private int biggestFolderIndex;

        public TempFolderContents(IFileSystem fileSystem, String tempFolderPath)
        {
            logger = LogManager.GetLogger(typeof(FileSystem));
            this.fileSystem = fileSystem;
            this.tempFolderPath = tempFolderPath;

            Refresh();
        }

        public int NextFolderIndex
        {
            get 
            {
                Refresh();
                return nextFolderIndex; 
            }
        }

        public int SmallestFolderIndex
        {
            get 
            {
                Refresh();
                return smallestFolderIndex; 
            }
        }

        public int BiggestFolderIndex
        {
            get 
            {
                Refresh();
                return biggestFolderIndex; 
            }
        }

        private void Refresh()
        {
            List<String> directories = fileSystem.GetDirectories(tempFolderPath);

            if (directories.Count > 0)
                AnalyzeFolders(directories);
            else
            {
                smallestFolderIndex = 0;
                biggestFolderIndex = 0;
                nextFolderIndex = 1;
            }
        }

        private void AnalyzeFolders(List<String> directories)
        {            
            bool isFirstDirectory = true;
            smallestFolderIndex = 0;
            biggestFolderIndex = 0;

            foreach (String directory in directories)
            {
                int currentIndex = 0;

                try
                {
                    logger.Debug("Analyzing temp folder " + directory);
                    String directoryName = System.IO.Path.GetFileName(directory);
                    currentIndex = int.Parse(directoryName);

                    if (isFirstDirectory)
                    {
                        smallestFolderIndex = currentIndex;
                        biggestFolderIndex = smallestFolderIndex;
                        isFirstDirectory = false;
                    }
                    else
                    {
                        if (currentIndex < smallestFolderIndex)
                            smallestFolderIndex = currentIndex;

                        if (currentIndex > biggestFolderIndex)
                            biggestFolderIndex = currentIndex;
                    }
                }
                // catch handlers for int.Parse
                // folders that have no numerical name are simply tolerated
                // but not taken into account when doing directory counting for rolling file buffer implementation
                catch (ArgumentNullException ex)
                {
                    logger.Warn("Failure while analyzing temp folders!", ex);                    
                }
                catch (FormatException ex)
                {
                    logger.Warn("Failure while analyzing temp folders!", ex);                    
                }
                catch (OverflowException ex)
                {
                    logger.Warn("Failure while analyzing temp folders!", ex);                    
                }                
            }

            nextFolderIndex = biggestFolderIndex + 1;

            System.Diagnostics.Debug.Assert(smallestFolderIndex > 0);
            System.Diagnostics.Debug.Assert(biggestFolderIndex > 0);
            System.Diagnostics.Debug.Assert(nextFolderIndex > biggestFolderIndex);
        }        
    }        
}
